/**
 * 军民大型国防科研仪器设备共享平台
 */
$(function () {

    // 是否构造下拉框数据，如果构造就不需要重新构造了
    var buildSelect = false;
    // 领域下拉框数据
    var domainSelectData = [];
    // 级别下拉框数据
    var levelSelectData = [];
    // 颜色
    var colorArray = ["rgba(164,66,241,.8)","rgba(50,154,255,.8)","rgba(0,208,208,.8)",
        "rgba(0,212,101,.8)","rgba(251,215,0,.8)","rgba(254,79,113,.8)"+
        "rgba(164,66,241,.8)","rgba(50,154,255,.8)","rgba(0,208,208,.8)",
        "rgba(0,212,101,.8)","rgba(251,215,0,.8)","rgba(254,79,113,.8)"];
    rebuildHtml();


    /**
     * 构建html
     */
    function rebuildHtml(){
        getSshareplatform();
    }

    /**
     * 获取平台数据
     */
    function getSshareplatform(){
        let params = {
            url: "/index/shareplatform",
            sCallback: function (data) {
                successCallback(data);
                // 构造下拉框
                // rebuildSelect();
                // buildSelect = true;
            },
            eCallback: function (res) {
                successCallback([]);
            }
        }
        requset(params);
    }

    function successCallback(data){
        var codeHtml = "";
        $.each(data, function (index, data) {
            // 如果没有构造下拉框数据，就需要构造
            var spDomain = data["spDomain"];
            if (!buildSelect) {
                var spLevel = data["spLevel"];
                buildSelectData(spDomain,spLevel);
            }
            var indexOf = domainSelectData.indexOf(spDomain);
            var color = colorArray[indexOf];
            var spLink = data["spLink"];
            var spName = data["spName"];
            spName = spName.replace(/\n/g,"<br/>");
            var spDomain = data["spDomain"];
            var spCompany = data["spCompany"];
            if (spLink != null && spLink != "" && spLink.length > 3) {
                spLink = " href= " + spLink;
            }
            codeHtml += "<li class='hex' > <a class='hexIn' "+spLink+" target='Blank' style='background-color:#eee;'>" +
                "<span style='height: 100%; width: 100%;  display: flex;align-items:center;justify-content:center;padding:2%;white-space:normal;'>"+spName+"</span>" +
                "<h1 style='color:white;font:2px;background-color:"+color+"'>"+spDomain+"</h1>" +
                "<p style='color:white;background-color: "+color+"'>"+spCompany+"</p>" +
                "</a> </li>"
        })
        $("#hexGrid").html(codeHtml);
    }

    // 构造下拉框数据
   function buildSelectData(spDomain,spLevel){
        // 如果数组中不存在就添加
       if (domainSelectData.indexOf(spDomain) == -1) {
           domainSelectData.push(spDomain);
       }
       if (levelSelectData.indexOf(spLevel) == -1) {
           levelSelectData.push(spLevel);
       }
   }
   // 构造下拉框页面
   function rebuildSelect(){
        var domainHtmlCode = "<option value='all'>----选择领域----</option>";;
       $.each(domainSelectData, function (index, value) {
           domainHtmlCode += "<option value=" + value + ">" + value + "</option>";
       });
       $("#domain-item").html(domainHtmlCode);
       // var levelHtmlCode = "<option value='all'>----选择级别----</option>";;
       // $.each(levelSelectData, function (index, value) {
       //     levelHtmlCode += "<option value=" + value + ">" + value + "</option>";
       // });
       // $("#level-item").html(levelHtmlCode);
    }

    /**
     * 查询事件
     */
    $("#search-btn").on("click",function(){
        var domainItem=$("#domain-item option:selected");
        var domain = domainItem.val();
        var levelItem=$("#level-item option:selected");
        var level = levelItem.val();
        if (domain == "all") {
            domain = "";
        }
        if (level == "all") {
            level = "";
        }
        searchData({domain:domain,level:level});
    })

    /**
     * 获取平台数据
     */
    function searchData(paramData){
        let params = {
            url: "/index/shareplatform",
            data: paramData,
            sCallback: function (data) {
                successCallback(data)
            },
            eCallback: function (res) {
                successCallback([]);
            }
        }
        requset(params);
    }

})
